package org.service.gateway.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.h2.tools.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * 
 * ClassName: ZuulConfig <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2019年10月14日 下午3:17:47 <br/>
 *
 * @author dengqian
 * @version
 * @since JDK 1.8
 */

 @ConditionalOnProperty(name = "spring.h2", havingValue = "enabled", matchIfMissing = false)
public class H2Config {
    private static final Logger logger = LoggerFactory.getLogger(H2Config.class);

//    private Server localServer;

    // @Bean
    // public DataSource dataSource() {
    // }
    // }

    @Bean
    public Server localServer() {
        Server localServer = null;

        try {
            if (localServer == null) {
                localServer = Server.createTcpServer(new String[] {"-tcpPort", "9095", "-tcpAllowOthers"}).start();
                if (localServer.isRunning(true)) {
                    logger.info("H2 server was started and is running.");
                } else {
                    throw new RuntimeException("Could not start H2 server.");
                }
            }
        } catch (SQLException localSQLException) {
            throw new RuntimeException("Failed to start H2 server: ", localSQLException);
        }
        return localServer;
    }

//    private void startH2Server() {
//
//    }
}
